var ClubTableInfoWindow = Window.extend({
    _ccsFileName: res.ClubTableInfoUnit_json,
    _roomInfo: null,
    _clubId: null,
    _blockLayerOpacity: null,
    _isManager: false,
    initWindow: function (data, clubId, isManager, wanfaInfo) {
        wanfaInfo.currentPlayerNum;
        wanfaInfo.maxPlayerNum;
        this._roomInfo = data['roomInfo'];
        this._clubId = clubId;
        this._nodeList['btn_invite'].setVisible(false);

        this._nodeList['txt_wanfa'].setString(wanfaInfo.name);
        this._isManager = isManager;
        if (isManager){
            this._nodeList['btn_join'].setVisible(true);
            this._nodeList['btn_jiesan'].setVisible(true);
            this._nodeList['btn_join_1'].setVisible(false);
        }
        else {
            this._nodeList['btn_join'].setVisible(false);
            this._nodeList['btn_jiesan'].setVisible(false);
            this._nodeList['btn_join_1'].setVisible(true);
        }
        if (wanfaInfo.roomInfo && wanfaInfo.roomInfo.option) {
            var option = JSON.parse(wanfaInfo.roomInfo.option);
            this._nodeList['txt_wanfa_desp'].setString(this._handleWanfaDesp(option.desp));
        } else if (wanfaInfo.desc) {
            this._nodeList['txt_wanfa_desp'].setString(this._handleWanfaDesp(wanfaInfo.desc));
        }
        this._refreshRoomInfoView();
    },

    _initClickListeners: function () {
        this._clickListeners = {
            'back': '_clickBg',
            'back_left': '_clickBg',
            'back_right': '_clickBg',
            'btn_join': '_clickJoinRoom',
            'btn_join_1': '_clickJoinRoom',
            'btn_invite': '_clickInvite',
            'btn_jiesan': '_clickJieSan',
            'touchLayer': '_clickTouchLayer',
        }
    },

    /**
     * 监听亲友圈消息
     * @private
     */
    _initEventListeners: function () {
        this._eventListeners = {};
        //监听 消息 事件
        this._eventListeners[ClubMsgCode.ListClubRoom] = '_listClubRoomHandler';
    },

    _listClubRoomHandler: function () {
        var roomList = ClubDataManager.getClubFloorRomList(this._clubId);
        var waitRoomList = ClubDataManager.getFormatClubRoomList(roomList, ClubTableItemType.Type_WaitJoin);
        var startRoomList = ClubDataManager.getFormatClubRoomList(roomList, ClubTableItemType.Type_StartRoom);
        roomList = waitRoomList.concat(startRoomList);
        for (var i = 0; i < roomList.length; i++) {
            if (roomList[i].roomInfo.room_id == this._roomInfo.room_id) {
                this._roomInfo = roomList[i].roomInfo;
                this._refreshRoomInfoView();
                return;
            }
        }
        this._clickTouchLayer();
    },


    _refreshRoomInfoView: function () {
        var uids = this._roomInfo['uids'];
        var resultscore = this._roomInfo['resultscore'] || [];
        for (var i = 0; i < 9; i++) {
            if (uids[i]) {
                this._nodeList['headNode.head' + i].setVisible(true);
                this._nodeList['headNode.head' + i + '.sp_w2j'].setVisible(false);
                var head = this._roomInfo['heads'][i] || res.defaultHead;
                var nickname = this._roomInfo['nicknames'][i] || uids[i];
                var score = uids[i] || 0;
                loadImageToSprite(head, this._nodeList['headNode.head' + i + '.head']);
                this._nodeList['headNode.head' + i + '.nickName'].setString(ellipsisStr(nickname, ClubDataManager.NirckNameSize));
                this._nodeList['headNode.head' + i + '.playerScore'].setString('ID: ' + score);
                /*
                if (score > 0) {
                    this._nodeList['headNode.head' + i + '.playerScore'].setTextColor(cc.color('#e62500'));
                } else {
                    this._nodeList['headNode.head' + i + '.playerScore'].setTextColor(cc.color('#299a3a'));
                }
                 */
            } else {
                this._nodeList['headNode.head' + i].setVisible(false);
            }
        }
        for (var i = this._roomInfo.cur_player_cnt; i < this._roomInfo.max_player_cnt; i++){
            this._nodeList['headNode.head' + i].setVisible(true);
            this._nodeList['headNode.head' + i + '.sp_w2j'].setVisible(true);
            this._nodeList['headNode.head' + i + '.nickName'].setString('');
            this._nodeList['headNode.head' + i + '.playerScore'].setString('');
        }
        var option = null;
        try {
            option = JSON.parse(this._roomInfo.option);
        } catch (e) {
            option = this._roomInfo.option;
        }


        var used = this._roomInfo.used || 0;
        var current_rounds = option.mapid >= 300 ? (!used ? 1 : (used + 1)) : used;
        var total_round = (option.rounds || option.jushu);
        var str = current_rounds + '/' + total_round;
        this._nodeList['createTime'].setString(timestamp2time(this._roomInfo['create_time']));
        this._nodeList['roomId'].setString('房号:' + this._roomInfo['room_id']);
        this._nodeList['round'].setString('局数:' + str);

        this._nodeList['btn_join'].setVisible(false);
        this._nodeList['btn_invite'].setVisible(false);
        this._nodeList['btn_join_1'].setVisible(false);
        if (this._roomInfo.status == ClubTableItemType.Type_WaitJoin) {
            if (this._isManager == true) {
                this._nodeList['btn_join'].setVisible(true);
            }
            else {
                this._nodeList['btn_join_1'].setVisible(true);
            }
            //this._nodeList['btn_invite'].setVisible(true);
        }
    },

    _clickBg: function () {

    },

    _clickJoinRoom: function () {
        clubMsgManager.requestJoinRoom(this._roomInfo.room_id);
        this._clickTouchLayer();
    },

    _clickInvite: function () {
        showWindow('ClubAssistantInviteWindow', this._clubId, this._roomInfo);
    },

    _clickJieSan: function () {
        var self = this;
        showMessageBox('是否确定解散该房间？', function () {
            network.send(3019, {
                daikai_room_id: self._roomInfo.room_id,
                room_id: self._roomInfo.room_id,
                force: true
            });
            self._clickTouchLayer();
        }, function () {

        });
    },

    _clickTouchLayer: function () {
        this.removeFromParent(true);
    },

    _handleWanfaDesp: function (wanfaDesp){
        var removedStrArr =['王钓麻将', '落地扫', '永州扯胡子', '2人PK', '3人激情', '2人玩', '3人玩', '4人玩',
            '群主支付', '房主支付', 'AA支付'];
        var retVal = wanfaDesp;
        var retValArr = retVal.split(',');
        var locationArr = [];
        for (var i = 0; i < retValArr.length; i++){
            retValArr[i] = retValArr[i].trim();
            if (removedStrArr.indexOf(retValArr[i]) > -1) locationArr.push(i);
        }

        for (var i = locationArr.length - 1; i > -1; i--){
            retValArr.splice(locationArr[i], 1);
        }

        retVal = retValArr.join(',');

        return retVal;
    },
});